﻿function contestProblemList(cid) {
	if (refreshTab('contestProblemList'))
		return;
	if (cid == null)
		return;
	var store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : url.contestProblemList
						}),
				reader : new Ext.data.JsonReader({
							root : 'result',
							totalProperty : 'count',
							id : 'ps'
						}, ['ps', 'title', 'ac', 'submit', 'status'])
			});
	store.load({
				params : {
					cid : cid,
					god : sys.god,
					player : sys.user.username
				}
			});
	var cm = new Ext.grid.ColumnModel([{
				header : 'Problem Title',
				sortable : false,
				dataIndex : 'title',
				width : 500,
				renderer : renTitle
			}, {
				header : 'Statistics',
				sortable : false,
				dataIndex : 'submit',
				width : 300,
				renderer : renStatistics
			}])

	var sm = new Ext.grid.RowSelectionModel({
				singleSelect : true
			});

	var toolbar = new Ext.Toolbar({
				items : [
						new Ext.Button({
									text : "刷新",
									iconCls : 'refresh-icon',
									handler : function() {
										store.reload();
									}
								}),
						new Ext.Button({
									text : "排名",
									iconCls : 'problemlist-icon',
									handler : function() {
										showContestRanklist(cid, store
														.getCount());
									}
								}), new Ext.Button({
									disabled : true,
									text : "进入",
									handler : onShowProblem
								}), new Ext.Toolbar.Separator(),
						new Ext.Button({
									text : '统计',
									iconCls : 'statistics-icon',
									disabled : true,
									handler : function() {
										var record = getSelect();
										alert("该功能还未完善");
										// showStatistics(record.data.pid);
									}
								}), new Ext.Button({
									text : '比赛当前状态',
									iconCls : 'preview-icon',
									handler : function() {
										getContestStatus(cid);
									}
								}), new Ext.Toolbar.Separator()]
			});

	if (sys.god) {
		var godbuttons = [new Ext.Button({
							text : "添加题目",
							handler : onAdd,
							iconCls : 'add-icon'
						}), new Ext.Button({
							text : "编辑题目",
							iconCls : 'edit-icon',
							handler : onEdit,
							disabled : true
						}), new Ext.Button({
							text : "删除题目",
							iconCls : 'delete-icon',
							handler : onDelete,
							disabled : true
						})];
		toolbar.add(godbuttons);
	}

	var grid = new Ext.grid.GridPanel({
				store : store,
				cm : cm,
				sm : sm,
				tbar : toolbar,
				viewConfig : {
					forceFit : true
				},
				loadMask : {
					msg : '载入中...'
				},
				region : 'center',
				id : 'contestProblemList',
				closable : true,
				title : 'ContestProblemList'
			});
	ui.contestProblemList = grid;
	sm.on('selectionchange', onSelect);
	grid.on('rowdblclick', onShowProblem);
	addTab(grid);

	function getSelect() {// 选择触发器，返回选择的record
		if (sm.getCount() == 0)
			return null;
		return sm.getSelected();
	}

	function onSelect() {
		var record = getSelect();
		var disable = !record;
		toolbar.get(2).setDisabled(disable);
		toolbar.get(3).setDisabled(disable);
		toolbar.get(4).setDisabled(disable);
		if (sys.god) {
			godbuttons[1].setDisabled(disable);
			godbuttons[2].setDisabled(disable);
		}
	}

	function renTitle(value, p, record) {
		return String
				.format(
						'<div class="{2}" style="height:16px">{2}.&nbsp; {0}'
								+ '<br />', value, ['problem-icon',
								'solving-icon', 'accepted-icon', 'hidden-icon'][record.data.status], record.data.ps, record.data.cid);
	}
	function renStatistics(value, p, record) {
		return String.format('{0} / {1}&nbsp; (<span class="ratio-text"> {2} </span>)',
				record.data.ac, value, sys.rd.ratio(record.data.ac, value));
	}

	function onAdd() {
		var params = {
			cid : cid
		}
		conAEProblem(params);
	}
	function onEdit() {
		var record = getSelect();
		
		var params = {
			cid : cid,
			ps : record.data.ps
		}
		conAEProblem(params);
	}
	function onShowProblem() {
		var record = getSelect();
		if (!record)
			return;
		contestProblem(cid, record.data.ps)
	}
	function onDelete() {
		var record = getSelect();
		if (!record)
			return;
		var params = {
			cid : cid,
			ps : record.data.ps,
			success : onRefreshCP
		};
		deleteCProblem(params);
	}
}

function onRefreshCP() {
	var grid = ui.contestProblemList;
	var store = grid.store;
	store.reload();
}
